package com.example.terminal.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.terminal.R;
import com.example.terminal.adapter.StoreListDetailsAdapter;
import com.example.terminal.base.BaseActivity;
import com.example.terminal.bean.CommonBean;
import com.example.terminal.bean.StoreListDetailsBean;
import com.example.terminal.bean.StoreListDetailsBean.DataBean.TstoregeInDeviceBean.tstoregeInDeviceDetailListBean;
import com.example.terminal.bean.StoreroomListBean;
import com.example.terminal.global.Constant;
import com.example.terminal.global.ConstantInfo;
import com.example.terminal.http.NetworkUtils;
import com.example.terminal.http.network.OkGoBackListener;
import com.example.terminal.listener.OnItemClickListener;
import com.example.terminal.util.CommonUtils;
import com.example.terminal.util.DimenUtils;
import com.example.terminal.util.ToastUtils;
import com.example.terminal.view.dialog.AlertDialog;
import com.example.terminal.view.dialog.StoreroomDialog;
import com.example.terminal.view.edit.EditorActivity;
import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.io.Serializable;
import java.util.List;

/**
 * 入库办理--详情页面
 */
public class StoreDetailsActivity extends BaseActivity {

    private TwinklingRefreshLayout mRefreshLayout;
    private RecyclerView mRvContainer;
    private RelativeLayout mRlContentNull;
    private TextView mTvTitleName, mTv_Store, mTvContent;
    private ImageButton mIbBack;

    private StoreListDetailsBean storeListDetailsBean;
    private List<StoreListDetailsBean.DataBean.TstoregeInDeviceBean> tstoregeInDevice;

    private StoreListDetailsAdapter storeListDetailsAdapter;

    private int mId;
    private String mOrderName, mStoreHouseName;
    private String mStatus;

    @Override
    protected int setContentView() {
        return R.layout.activity_store_details;
    }

    @Override
    protected void initView() {
        mTvTitleName = findViewById(R.id.mTvTitleName);
        mTvContent = findViewById(R.id.mTvContent);
        mIbBack = findViewById(R.id.mIbBack);

        mRefreshLayout = findViewById(R.id.mRefreshLayout);
        mRvContainer = findViewById(R.id.mRvContainer);
        mRlContentNull = findViewById(R.id.mRlContentNull);
        mTv_Store = findViewById(R.id.mTv_Store);//办理入库


    }

    @Override
    protected void initData() {
        mId = mBundle.getInt(Constant.ID);
        mOrderName = mBundle.getString(Constant.INFO);
        mStoreHouseName = mBundle.getString(Constant.Value);
        mStatus = mBundle.getString(Constant.STATUS);

        mTvTitleName.setText(mOrderName);
        mTvContent.setText(mStoreHouseName);

        mTv_Store.setOnClickListener(this::onClickSort);
        mIbBack.setOnClickListener(this::onClickSort);

    }

    @Override
    protected void initConnect() {

        NetworkUtils.getStoreDetails(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                storeListDetailsBean = (StoreListDetailsBean) body;
                tstoregeInDevice = storeListDetailsBean.getData().getTstoregeInDevice();
                initAdapter();
            }
        }, mId);
    }

    private void initAdapter() {
        if (storeListDetailsBean == null)
            return;


        mRlContentNull.setVisibility(tstoregeInDevice == null || tstoregeInDevice.size() == 0 ? View.VISIBLE : View.GONE);
        mRefreshLayout.setVisibility(tstoregeInDevice == null || tstoregeInDevice.size() == 0 ? View.GONE : View.VISIBLE);


        if (storeListDetailsAdapter == null) {
            storeListDetailsAdapter = new StoreListDetailsAdapter(mActivity, tstoregeInDevice);
            mRvContainer.setAdapter(storeListDetailsAdapter);
            mRvContainer.setLayoutManager(new LinearLayoutManager(mActivity));

            storeListDetailsAdapter.setOnItemClickListener(new DetailsItemClickListener());
        } else {
            storeListDetailsAdapter.updateAdapter(tstoregeInDevice);
        }
    }


    /**
     * 条目点击事件
     */
    private class DetailsItemClickListener extends OnItemClickListener {
        @Override
        public void onClick(View view, int position) {
            mIntent = new Intent(mActivity, StoreScanActivity.class);
            mBundle = new Bundle();
            mBundle.putInt(Constant.TurnType, position);
            mBundle.putSerializable(Constant.INFO, storeListDetailsBean);
            mIntent.putExtras(mBundle);
            turnActForResult(mIntent, 450);
        }
    }

    public void onClickSort(View view) {
        switch (view.getId()) {
            case R.id.mIbBack:
                onFinish();
                break;
            case R.id.mTv_Store:
                if (!isInFn())
                    return;

                AlertDialog.getInstance(mActivity)
                        .setContent("是否确认办理入库操作？")
                        .setConfirmButton(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                onSubmits("4");
                            }
                        }).show();

                break;

        }
    }

    private boolean isInFn() {
        if (tstoregeInDevice != null)
            for (int i = 0; i < tstoregeInDevice.size(); i++) {
                int storeDeviceNum = tstoregeInDevice.get(i).getStoreDeviceNum();
                List<tstoregeInDeviceDetailListBean> tstoregeInDeviceDetailList = tstoregeInDevice.get(i).getTstoregeInDeviceDetailList();

                if (tstoregeInDeviceDetailList == null || tstoregeInDeviceDetailList.size() == 0) {
                    ToastUtils.showToast("设备明细没添加！");
                    return false;
                }

                String imeiStatus = tstoregeInDevice.get(i).getImeiStatus();

                if ("1".equals(imeiStatus)) {
                    if (storeDeviceNum != tstoregeInDeviceDetailList.size()) {
                        ToastUtils.showToast("设备" + tstoregeInDevice.get(i).getDeviceName() + "的数量不一致!");
                        return false;
                    }
                } else {
                    if (storeDeviceNum != tstoregeInDeviceDetailList.get(0).getNum()) {
                        ToastUtils.showToast("设备" + tstoregeInDevice.get(i).getDeviceName() + "的数量不一致!");
                        return false;
                    }
                }


            }

        return true;
    }


    private void onSubmits(String status) {
        storeListDetailsBean.getData().setStatus(status);

        NetworkUtils.getStoreSave(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                CommonBean commonBean = (CommonBean) body;
                if (commonBean.getCode() == 200) {
                    ToastUtils.showToast(commonBean.getMsg());
                    onFinish();
                }
            }
        }, storeListDetailsBean.getData());
    }

    // 处理返回结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 450 && resultCode == 550) {
            List<tstoregeInDeviceDetailListBean> tstoregeInDeviceDetailList = (List<tstoregeInDeviceDetailListBean>) data.getSerializableExtra(Constant.INFO);
            int mPosition = data.getIntExtra(Constant.POSITION, 0);
            int Volume = data.getIntExtra(Constant.Volume, 0);

            tstoregeInDevice.get(mPosition).setStoreDeviceNum(Volume);
            tstoregeInDevice.get(mPosition).setTstoregeInDeviceDetailList(tstoregeInDeviceDetailList);

            initAdapter();
        }
    }
}